import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class RefreshIndicatorDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => RefreshIndicatorState();
}

class RefreshIndicatorState extends State<RefreshIndicatorDemo> {
  //数据列表
  List list_Data = [];
  int index = 0;

  @override
  void initState() {
    super.initState();
    //初始化数据
    getData();
  }

  /**
   * 获取数据
   */
  Future getData() async {
    await Future.delayed(Duration(seconds: 2), () {
      setState(() {
        list_Data = List.generate(20, (index) => '第$index个');
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('RefreshIndicator 下拉刷新'),),
      body: RefreshIndicator(
        child:ListView.builder(
          itemCount: list_Data.length,
          itemBuilder: (BuildContext context, int index) {
            return ListTile(
              title: Text(this.list_Data[index]),
            );
          },
        ),
        onRefresh:_onRefresh,
        // displacement:30,
        color:Colors.red,
        backgroundColor:Colors.green,
        // notificationPredicate:,
        semanticsLabel:'哈哈',
        semanticsValue:'sdfjnlsdjf',
      ),
    );
  }

  // 下拉监听 处理
  Future<Null> _onRefresh() async {
    index++;
    await Future.delayed(Duration(seconds: 3), () {
      print('refresh');
      setState(() {
        list_Data = List.generate(20, (i) => '第 $index 次刷新后数据 $i');
      });
    });
  }

}